package pikes.peak;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import pikes.html.xhtml.Flow;
import pikes.html.xhtml.PCData;
import pikes.html.xhtml.table.TD;
import pikes.html.xhtml.table.TH;
import pikes.html.xhtml.table.TableCell;
import pikes.peak.Cell;
import pikes.peak.CellDecorator;
import pikes.peak.Component;
import pikes.peak.test.AssertHtml;
import pikes.peak.test.PikesPeakComponentTestCase;

import org.springframework.web.servlet.support.RequestContext;

public class CellTestCase extends PikesPeakComponentTestCase {

	@Test
	public void simpleCell() throws Exception {
		Cell cell = new Cell();

		TableCell actual = cell.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertNull(actual);
	}

	@Test
	public void simpleCellWithNullComponent() throws Exception {
		Cell cell = new Cell(new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				// TODO Auto-generated method stub
				return null;
			}

		});

		TableCell actual = cell.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertNull(actual);
	}

	@Test
	public void simpleCellWithComponent() throws Exception {
		Cell cell = new Cell(new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				return new PCData("boo");
			}

		});

		TD expected = new TD(new PCData("boo"));

		TableCell actual = cell.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleHeaderCellWithComponent() throws Exception {
		Cell cell = new Cell(true, new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				return new PCData("boo");
			}

		});

		TH expected = new TH(new PCData("boo"));

		TableCell actual = cell.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleCellWithComponentArray() throws Exception {
		Component[] compArray = new Component[] { new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				return new PCData("boo");
			}

		} };
		Cell cell = new Cell(compArray);

		TD expected = new TD(new PCData("boo"));

		TableCell actual = cell.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleCellWithComponentArray1() throws Exception {
		Component[] compArray = new Component[] { new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				return new PCData("boo1");
			}

		}, new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				return new PCData("boo2");
			}

		} };
		Cell cell = new Cell(compArray);

		TD expected = new TD(new PCData("boo1"));
		expected.add("boo2");

		TableCell actual = cell.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleCellWithComponentWithDecoration() throws Exception {
		Cell cell = new Cell(new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				return new PCData("boo");
			}

		}, new CellDecorator() {
			public void decorateCell(TableCell cell, Map model, RequestContext requestContext) throws Exception {
				cell.setAlign(TableCell.Align.LEFT);
			}
		});

		TD expected = new TD(new PCData("boo"));
		expected.setAlign(TableCell.Align.LEFT);

		TableCell actual = cell.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}
}
